<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Http\Requests\Admin\GoodsRequest;
use App\Http\Requests\Admin\ImageRequest;
use App\Models\Category;
use App\Models\Good;
use App\Services\BusinessServices\QiniuImageUploadService;
use App\Transformers\GoodTransformer;
use Illuminate\Http\Request;

class GoodsController extends BaseController
{
    /**
     * 商品列表
     */
    public function index(Request $request)
    {
        //搜索标题
        $title = $request->input('title');
        //搜索分类
        $category_id = $request->input('category_id');
        //是否推荐
        $is_recommend = $request->input('is_recommend', false);
        //是否上架
        $is_on = $request->input('is_on', false);

        $goods = Good::when($title, function($query) use ($title) {
            $query->where('title', 'like', '%' . $title . '%');
        })->when($category_id, function($query) use ($category_id) {
            $query->where('category_id', $category_id);
        })->when($is_recommend !== false, function($query) use ($is_recommend) {
            $query->where('is_recommend', $is_recommend);
        })->when($is_on !== false, function($query) use ($is_on) {
            $query->where('is_on', $is_on);
        })->paginate($request->size);
        return $this->response->paginator($goods, new GoodTransformer());
    }

    /**
     * 添加商品
     */
    public function store(GoodsRequest $request)
    {
        //分类只能选择三级分类
        $category = Category::find($request->input('category_id'));
        if (!$category)
            return $this->response->errorBadRequest('分类不存在');
        if ($category->status == 0)
            return $this->response->errorBadRequest('分类被禁用');
        if ($category->level != 3)
            return $this->response->errorBadRequest('分类只能选择三级分类');
        $user_id = auth('api')->user()->id;
        //追加user_id字段
        $request->merge(['user_id' => $user_id]);
        Good::create($request->all());
        return $this->response->created();
    }

    /**
     * 商品详情
     */
    public function show(Good $good)
    {
       return $this->response->item($good, new GoodTransformer());
    }

    /**
     * 更新商品
     */
    public function update(GoodsRequest $request, Good $good)
    {
        //分类只能选择三级分类
        $category = Category::find($request->input('category_id'));
        if (!$category)
            return $this->response->errorBadRequest('分类不存在');
        if ($category->status == 0)
            return $this->response->errorBadRequest('分类被禁用');
        if ($category->level != 3)
            return $this->response->errorBadRequest('分类只能选择三级分类');
        $good->update($request->all());
        return $this->response->noContent();
    }

    /**
     * 是否上架
     */
    public function isOn(Good $good)
    {
        $good->is_on = $good->is_on == 0 ? 1 : 0;
        $good->save();
        return $this->response->noContent();
    }

    /**
     * 是否推荐
     */
    public function isRecommend(Good $good)
    {
        $good->is_recommend = $good->is_recommend == 0 ? 1 : 0;
        $good->save();
        return $this->response->noContent();
    }

}
